<?php

class ToolController extends Fengqiaoju_Controller_Action {

	public function init() {

		parent::init ();
	}

	public function uploadAction() {

		$this->_helper->layout->disableLayout ();
		$this->_helper->viewRenderer->setNoRender ();
		
		$adapter = new Zend_File_Transfer_Adapter_Http ();
		
		$adapter->setDestination ( APPLICATION_PATH . '/../public' . $this->config ['upload'] ['dir'] );
		//上传配置
		$adapter->addValidator ( 'Extension', false, $this->config ['upload'] ['extension'] )->addValidator ( 'Size', false, array ( 
			'min' => $this->config ['upload'] ['minsize'] , 'max' => $this->config ['upload'] ['maxsize'] 
		) )->addValidator ( 'Count', false, array ( 
			'min' => intval ( $this->config ['upload'] ['mincount'] ) , 'max' => intval ( $this->config ['upload'] ['maxcount'] ) 
		) ); //上传文件数量
		

		$fileInfo = $adapter->getFileInfo (); //获取基本配置
		print_r ( $fileInfo );
		
		//重命名配置
		if ($fileInfo) {
			foreach ( $fileInfo as $val ) {
				$fname = $val ['name'];
			}
			$exts = @split ( "[/\\.]", $fname );
			$n = count ( $exts ) - 1;
			$extName = $exts [$n];
			$info ['filename'] = $exts [0];
		}
		$filename = md5 ( time () . $fileInfo ['fFile'] ['name'] ) . '.' . $extName; //重命名
		$adapter->addFilter ( 'Rename', array ( 
			'target' => $filename , 'overwrite' => true 
		) ); //执行重命名
		

		//返回上传后出现在信息
		if (! $adapter->receive ()) {
			$messages = $adapter->getMessages (); //检测
			//Zend_Debug::dump($messages);
			$message = '';
			if (is_array ( $messages )) {
				foreach ( $messages as $k => $v ) {
					$message .= $k . ':' . $v . '<br>';
				}
			}
			else {
				$message = $messages;
			}
			$info ['message'] = $message;
		}
		else {
			$info ['filepath'] = $this->config ['upload'] ['dir'];
			$info ['message'] = '上传成功';
		}
		return $info;
	}

}

